/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.seagate.app.servlet;

import com.seagate.app.manager.UserRole;

import com.seagate.app.bean.UserBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author 452530
 */
public class UserTable extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
       response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        PrintWriter out = response.getWriter();
        try {

            UserRole pnc = new UserRole();
            List userList = pnc.getUserList();
            out.println("<table bgcolor=\"#CCCCCC\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
          out.println("<tr><td>");
          out.println("<table bgcolor=\"#6699CC\" cellpadding=\"3\" cellspacing=\"0\" border=\"0\" width=\"100%\">");

          out.println("<tr bgcolor=\"#eeeeee\">");
          out.println("<td>");
          out.println("<table bgcolor=\"#cccccc\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">");
          out.println("<tr><td>");
          out.println("<table bgcolor=\"#cccccc\" cellpadding=\"3\" cellspacing=\"1\" border=\"0\" width=\"100%\">");

          out.println("<tr bgcolor=\"#99CC66\">");

          out.println("<td nowrap><font color=\"#ffffff\">&nbsp;GID&nbsp;</font></td>");
          out.println("<td nowrap><font color=\"#ffffff\">&nbsp;USER ROLE&nbsp;</font></td>");

          out.println("<td nowrap><font color=\"#ffffff\">&nbsp;ACTION&nbsp;</font></td>");


          out.println("</tr>");
          for (int i = 0; i < userList.size(); i++) {
              UserBean userBean = (UserBean) userList.get(i);
              if(i%2==0){
              out.println("<tr bgcolor=\"#ffffff\">");
              }else{
              out.println("<tr bgcolor=\"#ccffff\">");
              }
              out.println("<td>&nbsp;"+userBean.getGid()+"&nbsp;</td>");
              if(userBean.getRole().equalsIgnoreCase("sa")){
                 out.println("<td>&nbsp;System Admin &nbsp;</td>");
              }else{
                 out.println("<td>&nbsp;Admin &nbsp;</td>");
              }



              out.println("<td><input name=\"imgEdit\" id=\"imgEdit\" src=\"./images/buttons/btn_edit.gif\" onclick=\"editUser('"+userBean.getGid()+"','"+userBean.getRole()+"')\"  border=\"0\"  type=\"image\"> | <input name=\"imgDel\" id=\"imgDel\" src=\"./images/buttons/btn_delete.gif\" onclick=\"deleteUser('"+userBean.getGid()+"')\" border=\"0\" type=\"image\"></td>");

              out.println("</tr>");

          }

          out.println("</table>");
          out.println("</td></tr>");
          out.println("</table>");
          out.println("</td>");
          out.println("</tr>");

          out.println("</table>");
          out.println("</td></tr>");
          out.println("</table>");


        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
